<?php


namespace Admin\Controller;

use Think\Controller;

class GoodsController extends AdminController
{
    public function showList()
    {
        $goodsModel = D('goods');
        $count = $goodsModel->count();// 查询满足要求的总记录数
        $Page = new \Think\Page($count, 5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->rollPage=3;
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $Page->setConfig('first','首页');
        $Page->setConfig('theme','%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');

        $show = $Page->show();// 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $goodsModel->order('goods_id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('goods', $list);// 赋值数据集
        $this->assign('page', $show);// 赋值分页输出

        $this->display();
    }

    public function add()
    {
        if (empty($_POST)) {
            $this->display();
        } else {
            $goods = D('goods');
            if (!empty($_FILES) && $_FILES['goods_image']['error'] == 0) {
                $info = $this->upload();
                dump($info);
                $goods->goods_big_img = ltrim($info['rootPath'], '.') . ltrim($info['goods_image']['savepath'], './') . $info['goods_image']['savename'];//服务器地址
                $im = new \Think\Image();
                $bigPath = $info['rootPath'] . ltrim($info['goods_image']['savepath'], './') . $info['goods_image']['savename'];//磁盘地址
                $smallPath = $info['rootPath'] . ltrim($info['goods_image']['savepath'], './') . 'thumb_' . $info['goods_image']['savename'];
                $im->open($bigPath);
                $im->thumb(100, 100);
                $im->save($smallPath);
                $goods->goods_small_img = ltrim($info['rootPath'], '.') . ltrim($info['goods_image']['savepath'], './') . 'thumb_' . $info['goods_image']['savename'];
            }
            $_POST['goods_create_time'] = time();
            $_POST['goods_small_img'] = $goods->goods_small_img;
            $_POST['goods_big_img'] = $goods->goods_big_img;
            $res = D('goods')->add($_POST);
            if ($res) {
                $this->redirect('showList', array(), 2, '添加商品成功');
            } else {
                $this->redirect('add', array(), 2, '添加商品失败');
            }
        }
    }

    public function upload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/';
        $upload->savePath = './'; // 设置附件上传目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息
            $this->error('上传失败！');
        } else {// 上传成功
            $info['rootPath'] = $upload->rootPath;
            return $info;
        }
    }

    public function edit($goods_id)
    {
        $goods = D('goods');
        if (empty($_POST)) {
            $item = $goods->find($goods_id);
            $this->assign('goods', $item);
            $this->display();
        } else {
            dump($_POST);
            $res = $goods->save($_POST);
            dump($res);
            if ($res) {
                $this->redirect('index', array(), 2, '修改商品成功');
            } else {
                $this->redirect('edit', array('goods_id' => $_POST['goods_id']), 2, '修改商品失败');
            }
        }
    }


    public function del($goods_id)
    {
        $res = D('goods')->delete($goods_id);
        if ($res !== false) {
            $this->redirect('index', array(), 2, '删除商品成功');
        } else {
            $this->redirect('del', array(), 2, '删除商品失败');
        }
    }

}